package com.example.floristshop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.floristshop.database.User;
import com.example.floristshop.utils.UserImageUtil;

import org.litepal.LitePal;
import org.w3c.dom.Text;

import de.hdodenhof.circleimageview.CircleImageView;


public class UserInfoFragment extends Fragment implements OnClickListener {

    private View rootView;
    private CircleImageView user_image_civ;
    private TextView modify_image_text;
    private EditText user_name_et, user_id_et;
    private Button modify_btn, logout_btn;

    private String new_name;
    private int new_image_id;

    private boolean modify_flag;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public UserInfoFragment() {

    }

    public static UserInfoFragment newInstance(String param1, String param2) {
        UserInfoFragment fragment = new UserInfoFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        modify_flag = false;
        preferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        editor = preferences.edit();

        // find view
        rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
        user_image_civ = (CircleImageView)rootView.findViewById(R.id.user_image);
        modify_image_text = (TextView)rootView.findViewById(R.id.modify_user_image_text);
        user_name_et = (EditText) rootView.findViewById(R.id.user_name);
        user_id_et = (EditText) rootView.findViewById(R.id.user_id);
        modify_btn = (Button)rootView.findViewById(R.id.modify_user_info_btn);
        logout_btn = (Button)rootView.findViewById(R.id.logout_btn);

        // set view
        user_image_civ.setImageResource(UserImageUtil.id_to_src(preferences.getInt("user_image_id", 0)));
        long id = preferences.getLong("user_id", 0);
        user_id_et.setText(id<0?(""+(-1)*id):(""+id));
        user_name_et.setText(preferences.getString("user_name", "未登录用户"));

        user_image_civ.setOnClickListener(this::onClick);
        user_id_et.setEnabled(false);
        user_name_et.setEnabled(false);
        modify_btn.setOnClickListener(this::onClick);
        logout_btn.setOnClickListener(this::onClick);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_image:
                if(modify_flag){
                    new_image_id = (new_image_id+1)%5;
                    user_image_civ.setImageResource(UserImageUtil.id_to_src(new_image_id));
                }
                break;

            case R.id.modify_user_info_btn:
                if (!modify_flag) {//点击“修改信息”后
                    user_name_et.setEnabled(true);
                    modify_image_text.setVisibility(View.VISIBLE);
                    modify_btn.setText("确认修改");
                    modify_btn.setTextColor(Color.WHITE);
                    modify_btn.setBackground(ResourcesCompat.getDrawable(getActivity().getResources(), R.drawable.drawable_modify_user_info_btn, null));
                } else {//点击“确认修改”后
                    new_name = user_name_et.getText().toString();

                    User user = new User();
                    user.setName(new_name);
                    user.setImage_id(new_image_id);
                    user.updateAll("user_id = ?", preferences.getLong("user_id", 0)+"");

                    editor.putString("user_name", new_name);
                    editor.putInt("user_image_id", new_image_id);
                    editor.apply();

                    user_name_et.setEnabled(false);
                    modify_image_text.setVisibility(View.INVISIBLE);
                    modify_btn.setText("修改信息");
                    modify_btn.setTextColor(Color.parseColor("#3fbac2"));
                    modify_btn.setBackground(ResourcesCompat.getDrawable(getActivity().getResources(), R.drawable.drawable_modify_user_info_btn2, null));
                }
                modify_flag = !modify_flag;
                break;

            case R.id.logout_btn:
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        SharedPreferences.Editor editor = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE).edit();
                        editor.clear().apply();

                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setTitle("提示");
                builder.setMessage("确定要退出该账号吗？");
                builder.show();
                break;
        }
    }

}